import 'package:bt_dsp_88/tool/assets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../../class/cross/ch_select.dart';
import '../../commont/base_controller.dart';
import '../../model/model.dart';
import '../../model/model_default.dart';
import '../../model/model_extension.dart';
import '../../notify/globalState.dart';
import 'cross_group.dart';
import 'pass.dart';

class CrossController extends BaseController {
  CrossController({super.key});
  @override
  State<CrossController> createState() => kstate = _CrossControllerState();
}

class _CrossControllerState extends BaseControllerState<CrossController> {
  late final List<List<GroupModel>> _list;
  int high_freq = 0;
  int low_freq = 0;
  @override
  void initState() {
    super.initState();
    _list = [group_list];
    high_freq = current_ch.high_pass!.freq;
    low_freq = current_ch.low_pass!.freq;
  }

  void _pass_Function(Passmodel pass, String tag) {
    switch (tag) {
      case 'High Pass':
      case 'Low Pass':
        if (current_ch.high_pass?.name == pass.name) {
          current_ch.high_pass?.select = true;
          current_ch.high_pass?.isBypass = false;
          current_ch.low_pass?.isBypass = false;
        } else {
          current_ch.low_pass?.select = true;
          current_ch.high_pass?.isBypass = false;
          current_ch.low_pass?.isBypass = false;
        }
        _send_data('FILTER', current_ch, pass);
      case 'ByPass':
        pass.isBypass = !pass.isBypass;
        ChannelModel copy_ch = current_ch.copy;
        Passmodel copypass = pass.copy;
        copypass.freq = copypass.ishigh ? copypass.min_freq : copypass.max_freq;
        if (pass.isBypass) {
          _send_data('FILTER', copy_ch, copypass);
        } else {
          _send_data('FILTER', current_ch, pass);
        }
      case 'Reset':
        pass.freq = pass.ishigh ? pass.min_freq : pass.max_freq;
        pass.isBypass = false;
        EasyLoading.showSuccess('Reset successfully');
        // _send_data('RESETPASS', current_ch, pass);
        _send_data('FILTER', current_ch, pass);
        break;
      case 'slope':
      case 'freq':
      case 'passtype':
        _send_data('FILTER', current_ch, pass);

      default:
    }
    current_ch.high_pass?.select = pass.name == current_ch.high_pass?.name;
    current_ch.low_pass?.select = pass.name == current_ch.low_pass?.name;

    unitChannel(current_ch);
  }

  @override
  void copy(ChannelModel model, ChannelModel from) {
    model.high_pass = from.high_pass?.copy;
    model.low_pass = from.low_pass?.copy;
  }

  @override
  Widget buildContent(BuildContext context) {
    return Column(children: [
      ChSelect(
          ch_list: globalModel.ch_list,
          onClick: (p0) {
            setState(() {
              unitChannel(p0);
            });
          }),
      PassView(pass: current_ch.high_pass!, onSendBlock: _pass_Function),
      PassView(pass: current_ch.low_pass!, onSendBlock: _pass_Function),
      SizedBox(height: 10 * GlobalConfig.scaleH),
      GroupView(
          list: _list, onClick: onclickGroup, onLongPress: onLongPressGroup),
    ]);
  }
}

///发送数据
extension SumControllerExtension on _CrossControllerState {
  void _send_data(String tag, ChannelModel ch, Passmodel pass) {
    if ((tag != 'RESETPASS' && tag != 'BYPASS' && tag != 'FILTER') ||
        ch.name == '') return;
    globalState.state = NotifyData(key: tag, message: '${ch.num}', body: pass);
    for (var group in select_group_list) {
      if (group.contain(ch)) {
        ChannelModel other_ch =
            group.ch1!.num == ch.num ? group.ch2! : group.ch1!;
        globalState.state =
            NotifyData(key: tag, message: '${other_ch.num}', body: pass);
      }
    }
  }
}
